// Copyright (c) Microsoft Corporation. All rights reserved.

namespace SPPivotViewer.HttpHandler.Internal
{
    using System;
    using System.Web;
    using System.Web.Caching;
    using SPPivotViewer.Utility;
    using Microsoft.SharePoint;


    /// <summary>
    /// A cache of collection objects, to use for servicing the subsequent requests for DZC and collection image tiles.
    /// This implementation just uses the global HttpRuntime Cache object. For a multi-server farm,
    /// use a centralized or distributed cache so that all the servers have access to the same cached objects.
    /// </summary>
    public class CollectionCache
    {
        // Public static methods
        //======================================================================

        public static CollectionCache Instance
        {
            get
            {
                if (null == s_instance)
                {
                    s_instance = new CollectionCache();
                }
                return s_instance;
            }
        }


        // Constructors, Finalizer and Dispose
        //======================================================================

        public CollectionCache()
        {
        }

        // Public Methods
        //======================================================================

        public void Add(string key, Collection collection)
        {
            string _strHourCache = PV4SPSettings.GetValue(SPContext.Current.Web, PV4SPSettings.cstHourCache);
            string _strMinuteCache = PV4SPSettings.GetValue(SPContext.Current.Web, PV4SPSettings.cstMinCache);
            string _strSecondeCache = PV4SPSettings.GetValue(SPContext.Current.Web, PV4SPSettings.cstSecCache);

            int _iHourCache = 0;
            int _iMinCache = 0;
            int _iSecCache = 0;

            bool _bHourCache = false;
            bool _bMinuteCache = false;
            bool _bSecCache = false;

            _bHourCache = int.TryParse(_strHourCache, out _iHourCache);
            _bMinuteCache = int.TryParse(_strMinuteCache, out _iMinCache);
            _bSecCache = int.TryParse(_strSecondeCache, out _iSecCache);


            TimeSpan tsExpire;
            if(_bSecCache && _bMinuteCache && _bHourCache)
                tsExpire = new TimeSpan(_iHourCache, _iMinCache, _iSecCache);
            else
                tsExpire=new TimeSpan(0,20,0);
            HttpRuntime.Cache.Insert(key, collection, null, Cache.NoAbsoluteExpiration, tsExpire);
        }

        public Collection Get(string key)
        {
            string keyDecode = HttpUtility.UrlDecode(key);
            object o = HttpRuntime.Cache.Get(keyDecode);
            return (Collection)o;
        }

        public void ClearCache()
        {
            foreach (string key in HttpRuntime.Cache)
            {
                HttpRuntime.Cache.Remove(key);
            }
        }

        // Private Fields
        //======================================================================

        /// <summary>
        /// The default cache expiration policy is set to a 20 minute sliding window.
        /// </summary>

        static CollectionCache s_instance;
    }

}
